import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store' // 引入vuex store实例
// import getPageTitle from '@/utils/get-page-title'
import TabBar from '@/views/TabBar.vue'
import { Dialog } from 'vant'

// 解决路由重复点击同一个路由出现的报错问题解决
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { err = '' })
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    redirect: '/home',
    component: TabBar,
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import('@/views/Home')
      },
      {
        path: '/video',
        name: 'Video',
        component: () => import('@/views/Video'),
        meta: { isLogin: true }
      },
      {
        path: '/qa',
        name: 'Qa',
        component: () => import('@/views/Qa')
      },
      {
        path: '/user',
        name: 'User',
        component: () => import('@/views/User')
      }

    ]
  },
  {
    path: '/user/information',
    name: 'Information',
    component: () => import('@/views/User/information.vue')
  },
  {
    path: '/user/mycontent',
    name: 'Mycontent',
    component: () => import('@/views/User/my-content.vue')
  },
  {
    path: '/download',
    name: 'Download',
    component: () => import('@/views/User/Download-Management.vue')
  },
  {
    path: '/searchsllcontent',
    name: 'SearchAllContent',
    component: () => import('@/views/User/SearchAllContent.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login')
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/Search')
  },
  {
    path: '/article/:id',
    name: 'Article',
    component: () => import('@/views/Article')
  },
  {
    path: '/setting',
    name: 'Setting',
    component: () => import('@/views/Setting')
  },
  {
    path: '/setting/profile',
    name: 'Profile',
    component: () => import('@/views/Setting/components/edit-profile')
  },
  {
    path: '/setting/account',
    name: 'Account',
    component: () => import('@/views/Setting/components/account-security')
  },
  {
    path: '/setting/privacy',
    name: 'Privacy',
    component: () => import('@/views/Setting/components/privacy-setting')
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404')
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由的前置守卫
router.beforeEach(async function (to, from, next) {
  // document.title = getPageTitle(to.meta.title)
  if (store.getters.token) {
    if (to.path === '/login') return next('/user')
    next() // 直接放行
  } else {
    // 判断去的页面是否需要登录
    if (to.meta.isLogin) {
      Dialog.confirm({
        title: '访问提示',
        message: '该功能需要登录才能访问，确认登录吗'
      }).then(() => {
        router.replace({
          name: 'Login',
          query: {
            redirect: router.currentRoute.fullPath
          }
        })
      }).catch(() => {
        next(false)
      })
    } else {
      // 当前页面不需要登录的页面直接过去
      next()
    }
  }
})
// 后置守卫
router.afterEach(function () {

})

export default router
